package com.bjtime.designer.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.bjtime.designer.R
import com.bjtime.designer.utils
import android.icu.lang.UCharacter.GraphemeClusterBreak.T


class RulerView : View {

    var fuckView : RulerView? = null

    var mType     = 0
    var DRAWSPAC  = 30f
    var DRAWWIDTH = 0f
    var DRAWHEIGHT = 0f
    var DRAWPIX = 60f
    var PIXSCALE = 0f
    var mPaint = Paint ()
    init {
        mPaint.color = -0x777778
        mPaint.strokeWidth = 1f
    }
    var mTextPaint = Paint ()
    init {
        mTextPaint.color = -0x777778
        mTextPaint.strokeWidth = 1f
        mTextPaint.textSize = 20f
    }

    constructor(context: Context) : this(context, null as AttributeSet)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        var a = context.obtainStyledAttributes(attrs, R.styleable.RulerView)
        DRAWPIX = a.getInt(R.styleable.RulerView_size, 60).toFloat()
        mType = a.getInt(R.styleable.RulerView_type, mType)
        //DRAWPIX = utils.mm2Px (context, DRAWPIX)
        a.recycle()
    }

    fun bindRulerView (v : RulerView) {
        fuckView = v
    }

    fun setSpac (spac : Float) {
        if (mType == 1) {
            DRAWSPAC = spac
            var layoutParams = layoutParams
            layoutParams.height =  (DRAWSPAC * getSize() + paddingTop).toInt()
        }
    }

    fun getSpac () : Float {
        return DRAWSPAC
    }

    fun setSize (size : Int) {
        DRAWPIX = size.toFloat()
        if (mType == 0) {
            super.requestLayout()
        }
        OnSizeCHange(0,0,0,0)
    }

    fun getSize () :Int {
        return DRAWPIX.toInt();
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        OnSizeCHange (w, h, oldw, oldh)
    }


    fun OnSizeCHange (w: Int, h: Int, oldw: Int, oldh: Int) {
        DRAWWIDTH = width.toFloat() - paddingRight.toFloat()
        DRAWHEIGHT = height.toFloat()
        if (mType == 0) {
            DRAWSPAC = DRAWWIDTH / DRAWPIX
            if (fuckView != null)
                fuckView!!.setSpac(DRAWSPAC)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (mType == 1)
            DrawVertical (canvas)
        else
            DrawHorizontal (canvas)
    }


    fun DrawVertical (canvas: Canvas?) {
        for (i in 0..DRAWPIX.toInt()) {
            if (i % 10 == 0) {
                mPaint.strokeWidth = 1.5f
                canvas?.drawLine(
                    width.toFloat()  - utils.dp2px (context,10f),
                    (i * DRAWSPAC + paddingTop),
                    width.toFloat(),
                    (i * DRAWSPAC + paddingTop),
                    mPaint
                )
                if (i == 0) {
                    canvas?.drawText(
                        "" + i,
                        width.toFloat() - utils.dp2px (context,20f),
                        (i * DRAWSPAC + paddingTop) - mTextPaint.measureText (""+i) / 2 ,
                        mTextPaint
                    )
                } else {
                    canvas?.drawText(
                        "" + i,
                        width.toFloat() - utils.dp2px (context,20f),
                        (i * DRAWSPAC + paddingTop) - mTextPaint.measureText (""+i) / 2,
                        mTextPaint
                    )
                }
            } else {
                if (i%5==0) {
                    mPaint.strokeWidth = 1.3f
                    canvas?.drawLine(
                        width.toFloat()  - utils.dp2px (context,6f),
                        (i * DRAWSPAC + paddingTop),
                        width.toFloat(),
                        (i * DRAWSPAC + paddingTop),
                        mPaint
                    )
                } else {
                    mPaint.strokeWidth = 1f
                    canvas?.drawLine(
                        width.toFloat()  - utils.dp2px (context,4f),
                        (i * DRAWSPAC + paddingTop),
                        width.toFloat(),
                        (i * DRAWSPAC + paddingTop),
                        mPaint
                    )
                }
            }
        }
    }



    fun DrawHorizontal (canvas: Canvas?) {
        for (i in 0..DRAWPIX.toInt()) {
            if (i%10==0) {
                mPaint.strokeWidth = 1.5f
                canvas?.drawLine(
                    (i * DRAWSPAC ),
                    height.toFloat() - utils.dp2px (context,10f),
                    (i * DRAWSPAC ) ,
                    height.toFloat(),
                    mPaint
                )

                if (i == 0) {
                    canvas?.drawText(
                        "" + i,
                        (i * DRAWSPAC) - mTextPaint.measureText (""+i) / 2 + 5,
                        height.toFloat() - utils.dp2px (context,20f),
                        mTextPaint
                    )
                } else {
                    canvas?.drawText(
                        "" + i,
                        (i * DRAWSPAC) - mTextPaint.measureText (""+i) / 2,
                        height.toFloat() - utils.dp2px (context,20f),
                        mTextPaint
                    )
                }

            } else {
                if (i%5==0) {
                    mPaint.strokeWidth = 1.3f
                    canvas?.drawLine(
                        (i * DRAWSPAC),
                        height.toFloat() - utils.dp2px (context,6f),
                        (i * DRAWSPAC ),
                        height.toFloat(),
                        mPaint
                    )
                } else {
                    mPaint.strokeWidth = 1f
                    canvas?.drawLine(
                        (i * DRAWSPAC ),
                        height.toFloat() - utils.dp2px (context,4f),
                        (i * DRAWSPAC ),
                        height.toFloat(),
                        mPaint
                    )
                }
            }

        }
    }
}